package com.ibm.ems.servlets;

import java.io.IOException;

import java.sql.Connection;
import java.util.ArrayList;



import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.ems.dao.TopicsDao;
import com.ibm.ems.dto.TopicBean;
import com.ibm.ems.util.ConnectionUtil;

/**
 * Servlet implementation class TopicServlet
 */
public class TopicServlet extends HttpServlet {
	Connection conn;
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public TopicServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		Connection conn = ConnectionUtil.getConnection();
		TopicsDao td=new TopicsDao(conn);
		
		
		if(request.getParameter("create")!= null)
		{
			TopicBean t1=new TopicBean();
			t1.setCategory(request.getParameter("Category"));
			t1.setTopicId(request.getParameter("TopicCode"));
			t1.setTopicName(request.getParameter("TopicName"));
			t1.setDescription(request.getParameter("Description"));
			
			td.TopicsDaoCreate(t1);
			
		}
		
//		try {
//			Class.forName("oracle.jdbc.OracleDriver");
//			conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle","hr","hr");
//		
//		
//} catch (SQLException e)
//		 {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (ClassNotFoundException e) 
//		{
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		
		
		
		//PreparedStatement pstmt = null;
		//ResultSet rs = null;
		if(request.getParameter("changestatus")!=null)
		{
			TopicBean pp=new TopicBean();
			pp.setStatus(request.getParameter("status"));
			pp.setTopicId(request.getParameter("topicid"));
			td.modifyStatus(pp);
									
			
		}
		
		
		
		System.out.println(request.getParameter("modify"));
	if(request.getParameter("modify")!=null)
	{
		
		
		TopicBean mod=new TopicBean();
		mod.setCategory(request.getParameter("Category"));
		mod.setDescription(request.getParameter("Description"));
		mod.setTopicId(request.getParameter("TopicCode"));
		mod.setTopicName(request.getParameter("TopicName"));

		td.modifydatabase(mod);
			
	}
	
		

	System.out.println(request.getParameter("create"));

//Topics t1=new Topics();
//t1.setTopic_code(request.getParameter("TopicCode"));
//t1.setCategory(request.getParameter("Category"));
//System.out.println("wassup1");
//try{
//	Date d=new Date(12, 12, 89);
//	System.out.println("wassup2");
//	
//pstmt=conn.prepareStatement("insert into employees values(?,?,?,?,?,?,?,?,?,?,?);");	
//System.out.println(t1.getCategory());
//System.out.println(t1.getTopic_code());
//pstmt.setInt(1, 225);
//pstmt.setString(5,"2");
//pstmt.setString(4,"asdsad");
//
//pstmt.setString(2,t1.getCategory());
//pstmt.setString(3, t1.getTopic_code());
//pstmt.setDate(6,d);
//pstmt.setString(7,"asdsd");
//
//
//
//rs=pstmt.executeQuery();
//pstmt=conn.prepareStatement("commit");
//rs=pstmt.executeQuery();
//
//
//}catch(SQLException e){}
//catch(Exception e)
//{}



				

//PreparedStatement pstmt = null;
//ResultSet rs = null;
ArrayList<TopicBean> l=new ArrayList<TopicBean>();
td.TopicsDaoInit(l);
RequestDispatcher rd=request.getRequestDispatcher("/topicpage.jsp");
request.setAttribute("a",l);
rd.forward(request, response);	
//System.out.println("wassup3");
//		try {
//			System.out.println("wassup4");
//			ArrayList<Topics> l=new ArrayList<Topics>();
//			//ArrayList<String> l1=new ArrayList<String>();
//			//ArrayList<String> l2=new ArrayList<String>();
//			//ArrayList<String> l3=new ArrayList<String>();
//			
//			
//			
//			try {
//				pstmt = conn.prepareStatement("select * from Employees");
//				int count=0;
//			Topics f=new Topics();
//				rs = pstmt.executeQuery();
//				RequestDispatcher rd=request.getRequestDispatcher("/topicpage.jsp");
//				while (rs.next())
//				{
//					f.setCategory(rs.getString("FIRST_NAME"));
//					f.setTopic_code(rs.getString("LAST_NAME"));											
//					
//					l.add(count, f);
//					
//					f=new Topics();
//					count++;
//							}
//					
//				
//				request.setAttribute("a",l);
//				rd.forward(request, response);	
//				
//				
//			} 
//			catch (SQLException ex)
//			{
//				throw ex;
//			} 
//			catch (ServletException ex)
//			{
//				
//				ex.printStackTrace();
//			} 
//			catch (IOException ex) 
//			{
//				// TODO Auto-generated catch block
//				ex.printStackTrace();
//			} finally {
//				try {
//					if (rs != null)
//						rs.close();
//					if (pstmt != null)
//						pstmt.close();
//				} catch (Exception ex1){
//					
//				}
//			}
//			
//			conn.close();
//		
//		
//		} catch (SQLException ex) {
//			// TODO Auto-generated catch block
//			ex.printStackTrace();
//		}
//		
//		
		
		
	}
	 

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		// TODO Auto-generated method stub
	}

}
